package com.dz.risk.aspect;

import com.dz.risk.annocation.SystemLog;
import com.dz.risk.pojo.domain.SystemLogEntity;
import com.dz.risk.service.SystemLogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 定义一个切面
 */
@Aspect
@Component
@Slf4j
public class ServiceLogAspect {

    @Autowired
    private SystemLogService logService;

    //切点
    @Pointcut("execution(* com.dz.risk.controller..*.*(..))")
    public void savelog() {
    }

    ;

    /**
     * 这里定义了一个获取注解的方法，就是为了获取Controller层的具体操作。
     * 是否存在注解， 如果存在就获取
     *
     * @param joinPoint
     */
    private static SystemLog getAnnotationLog(JoinPoint joinPoint) {

        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null) {
            //获得注解 @RiseLog
            return method.getAnnotation(SystemLog.class);
        }
        return null;
    }

    //方法的返回值注入给ret   @AfterReturning在切入点return内容之后切入内容：例如，一个方法没有抛出任何异常，正常返回
    @AfterReturning(returning = "ret", pointcut = "savelog()")
    public void doafter(JoinPoint joinPoint, Object ret) {
        //获得注解
        SystemLog controllerLog = getAnnotationLog(joinPoint);
        //获取servlet请求对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        if (controllerLog != null) {
            //获取注解中的参数值
            String operateName = controllerLog.operateName();
            SystemLogEntity logPojo = new SystemLogEntity();
            logPojo.setOperationContent(operateName);
            logPojo.setActionType(controllerLog.actionType());
            //添加日志操作
            logService.addStSystemLog(logPojo);
        }
    }


    //方法抛出异常时执行
    @AfterThrowing(pointcut = "savelog()", throwing = "ex")
    public void after(JoinPoint joinPoint, Exception ex) {
        //获得注解
        SystemLog controllerLog = getAnnotationLog(joinPoint);
        //获取servlet请求对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        if (controllerLog != null) {
            //获取注解中的参数值
            String operateName = controllerLog.operateName();
            SystemLogEntity logPojo = new SystemLogEntity();
            logPojo.setOperationContent(operateName);
            logPojo.setActionType(controllerLog.actionType());
            //添加日志操作
            logService.addStSystemLog(logPojo);
        }
    }
}


















